package api

import (
	"fmt"

	"gitee.com/liuxiaowei123/vblog-1.1/app/blog"
	"gitee.com/liuxiaowei123/vblog-1.1/app/token"
	"gitee.com/liuxiaowei123/vblog-1.1/app/user"
	"gitee.com/liuxiaowei123/vblog-1.1/middlewares"
	"gitee.com/liuxiaowei123/vblog-1.1/response"
	"github.com/gin-gonic/gin"
)

// 注册路由
func (b *BlogApiHandler) Registry(r gin.IRouter) {
	v1 := r.Group("/v1/blogs")
	// v1 := r.Group("v1").Group("blogs")
	v1.GET("/", b.QueryBlog)
	v1.GET("/:id", b.DescribeBlog)
	// 中间件校验
	v1.Use(middlewares.NewTokenAuthor().Author)
	v1.POST("/", middlewares.Required(user.ROLE_AUTHOR), b.CreateBlog)
	v1.PUT("/:id", middlewares.Required(user.ROLE_AUTHOR), b.UpdatePutBlog)
	v1.PATCH("/:id", middlewares.Required(user.ROLE_AUTHOR), b.UpdatePatchBlog)
	v1.DELETE("/:id", middlewares.Required(user.ROLE_AUTHOR), b.DeleteBlog)
	v1.POST("/:id", middlewares.Required(user.ROLE_AUDITOR), b.AuditBlog)
}

func (b *BlogApiHandler) CreateBlog(c *gin.Context) {
	tl := c.Keys[token.TOKEN_GIN_KEY_NAME]
	cook := tl.(*token.Token)

	// 获取用户请求
	req := blog.NewCreateBlogRequest()
	err := c.BindJSON(req)
	if err != nil {
		response.Failed(c, err)
	}
	req.CreateBy = cook.UserName

	// 处理请求
	ins, err := b.blogsvc.CreateBlog(c.Request.Context(), req)
	if err != nil {
		response.Failed(c, err)
	}

	// 返回响应
	response.Success(c, ins)
}

func (b *BlogApiHandler) QueryBlog(c *gin.Context) {
	// 获取
	req := blog.NewQueryBlogRequest()
	req.Keywords = c.Query("keywords")
	// s :=  c.Query("page_number")
	// i,_ := strconv.Atoi(s)
	// req.PageSize = i
	req.ParsePageSize(c.Query("page_size"))
	req.ParsePageNumber(c.Query("page_number"))
	switch c.Query("status") {
	case "draft":
		req.SetStatus(blog.STATUS_DRAFT)
	case "publised":
		req.SetStatus(blog.STATUS_PUBLISHE)
	}
	// err := c.BindJSON(req)
	// if err != nil {
	// 	response.Failed(c, err)
	// }

	// 处理
	ins, err := b.blogsvc.QueryBlog(c.Request.Context(), req)
	if err != nil {
		response.Failed(c, err)
	}

	// 响应
	response.Success(c, ins)
}

func (b *BlogApiHandler) DescribeBlog(c *gin.Context) {
	// 获取
	req := blog.NewDescribeBlogRequest(c.Param("id"))
	// err := c.BindJSON(req)
	// if err != nil {
	// 	response.Failed(c, err)
	// }

	// 处理
	ins, err := b.blogsvc.DescribeBlog(c.Request.Context(), req)
	if err != nil {
		response.Failed(c, err)
	}

	// 响应
	response.Success(c, ins)
}

func (b *BlogApiHandler) UpdatePutBlog(c *gin.Context) {
	// 获取
	req := blog.NewPutUpdateBlogRequest(c.Param("id"))
	err := c.BindJSON(req.CreateBlogRequest)
	if err != nil {
		response.Failed(c, err)
	}
	fmt.Println(req)
	// 处理
	ins, err := b.blogsvc.UpdateBlog(c.Request.Context(), req)
	if err != nil {
		response.Failed(c, err)
	}

	//响应
	response.Success(c, ins)
}

func (b *BlogApiHandler) UpdatePatchBlog(c *gin.Context) {
	// 获取
	req := blog.NewPatchUpdateBlogRequest(c.Param("id"))
	err := c.BindJSON(req)
	if err != nil {
		response.Failed(c, err)
	}

	// 处理
	ins, err := b.blogsvc.UpdateBlog(c.Request.Context(), req)
	if err != nil {
		response.Failed(c, err)
	}

	//响应
	response.Success(c, ins)
}

func (b *BlogApiHandler) DeleteBlog(c *gin.Context) {
	// 获取
	req := blog.NewDeleteBlogRequest(c.Param("id"))
	// err := c.BindJSON(req)
	// if err != nil {
	// 	response.Failed(c, err)
	// }

	// 处理
	err := b.blogsvc.DeleteBlog(c.Request.Context(), req)
	if err != nil {
		response.Failed(c, err)
	}

	// 响应
	response.Success(c, "OK")
}

func (b *BlogApiHandler) AuditBlog(c *gin.Context) {
	// 获取
	req := blog.NewAuditBlogRequest(c.Param("id"))
	// err := c.BindJSON(req)
	// if err != nil {
	// 	response.Failed(c, err)
	// }

	// 处理
	blog, err := b.blogsvc.AuditBlog(c.Request.Context(), req)
	if err != nil {
		response.Failed(c, err)
	}

	// 响应
	response.Success(c, blog)
}
